package com.share.example.stock.schedule;

import com.share.common.utils.DateUtil;
import com.share.example.stock.model.db.ScheduleLog;
import com.share.example.stock.model.db.StockBaseInfo;
import com.share.example.stock.service.IScheduleLogService;
import com.share.example.stock.service.IStockBaseInfoService;
import com.share.example.stock.service.ITradeDateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Slf4j
@Component
public class S10_Down_stock_code {
    private String className = this.getClass().getSimpleName();
    @Autowired
    private IScheduleLogService iScheduleLogService;

    @Autowired
    private IStockBaseInfoService iStockBaseInfoService;

    @Autowired
    private ITradeDateService iTradeDateService;

    @PostConstruct
    @Scheduled(cron = "0 */30 * * * ?")
    public void initStockInfo() {
        iStockBaseInfoService.initStockInfo();
    }

    //计算均值
    @Scheduled(cron = "0 0 8 * * ?")
    public void updateDn() {
        Date date = new Date();
        String strDate = DateUtil.getDate(date);
        if (!iTradeDateService.isTradeDay(date)) {
            log.info("非交易日直接退出");
            return;
        }

        String jobCode = className + "_" + "updateDn";

        //开始日志
        ScheduleLog insertLog = iScheduleLogService.insertLog(jobCode);

        //开始下载数据库
        ScheduleLog upLog = new ScheduleLog().setId(insertLog.getId());
        try {
            int i = iStockBaseInfoService.updateDn(strDate);
            upLog.setStatus("1");
            upLog.setRemark("更细条数=" + i);
        } catch (Exception e) {
            upLog.setExpMsg(e.getMessage());
            log.error("下载失败", e);
        }

        //更新日志
        iScheduleLogService.updateLogById(upLog);
    }

    //每天3点执行一次
    @Scheduled(cron = "10 35 3 * * ?")
    public void downAllStockCode() {
        String jobCode = className + "_" + "downAllCode";

        //开始日志
        ScheduleLog insertLog = iScheduleLogService.insertLog(jobCode);

        //开始下载数据库
        ScheduleLog upLog = new ScheduleLog().setId(insertLog.getId());
        try {
            Set<String> list = iStockBaseInfoService.downAllCode();
            upLog.setStatus("1");
            upLog.setRemark("数据条数=" + (list == null ? 0 : list.size()));
        } catch (Exception e) {
            upLog.setExpMsg(e.getMessage());
            log.error("下载失败", e);
        }

        //更新日志
        iScheduleLogService.updateLogById(upLog);
    }


    // 每5分钟检查一次
    @Scheduled(cron = "0 0/15 * * * ?")
    public void downAllStockInfo() {
        String jobCode = className + "_" + "downAllStockInfo";

        //检查数据是否存在
        List<String> codeSet = iStockBaseInfoService.listCodeByBasePullCount();
        if (CollectionUtils.isEmpty(codeSet)) {
            log.info("listCodeByBasePullCount无下载任务");
            return;
        }

        //开始日志
        ScheduleLog insertLog = iScheduleLogService.insertLog(jobCode);

        //开始下载数据库
        ScheduleLog upLog = new ScheduleLog().setId(insertLog.getId());
        try {
            iStockBaseInfoService.resetErrorCount(0);
            List<StockBaseInfo> list = iStockBaseInfoService.downAndUpdateStockInfo(codeSet);
            upLog.setStatus("1");
            upLog.setRemark("数据条数=" + (list == null ? 0 : list.size()));
        } catch (Exception e) {
            upLog.setExpMsg(e.getMessage());
            log.error("下载失败", e);
        }

        //更新日志
        iScheduleLogService.updateLogById(upLog);
    }
}
